//
//  CollectionRecordController.m
//  NtoccDriverNew
//
//  Created by 张天兴 on 2023/4/26.
//  Copyright © 2023 GCY. All rights reserved.
//

#import "CollectionRecordController.h"
#import "SearshBcTradeRecordVC.h"

#import "TradeRecordCell.h"
#import "BcTradeRecordCell.h"
@interface CollectionRecordController ()<UITableViewDataSource, UITableViewDelegate>
@property (strong , nonatomic)UITableView *tableView;

@property (strong , nonatomic)NSMutableArray *dataArray;

@property (strong , nonatomic)UIImageView *foundImageView;

@property (nonatomic, assign) NSInteger pageNum;

@property (nonatomic, strong) NSDictionary *searchResultDic;

@property (nonatomic, strong) UIButton *zhStatTimeButton;

@property (nonatomic, strong) UILabel *zhZhiLabel;

@property (nonatomic, strong) UIButton *zhEndTimeButton;

@property (nonatomic, strong)NSString *zhSStr;

@property (nonatomic, strong)NSString *zhSEnd;

@end
static NSString *const cellID = @"BankCardCellID";

@implementation CollectionRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    _pageNum = 1;
    
    [self setupBaseView];
    
    [self tradeRecordsListRequest];
}

- (void)setupBaseView {
    
    self.navigationItem.title = @"收款记录";
    
    self.view.backgroundColor = BackgroundColor;
    
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [searchButton setTitle:@"清空" forState:UIControlStateNormal];
    [searchButton setTitleColor:Dark_TextColor forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(rightBarItemClickAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:searchButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    self.zhSStr= @"";
   self.zhSEnd = @"";
    [self.view addSubview:self.zhStatTimeButton];
    [self.view addSubview:self.zhZhiLabel];
    [self.view addSubview:self.zhEndTimeButton];
    [self.view addSubview:self.tableView];
}

- (void)rightBarItemClickAction{
    [self.zhStatTimeButton setTitle:@"收款开始时间" forState:(UIControlStateNormal)];
    [self.zhEndTimeButton setTitle:@"收款结束时间" forState:(UIControlStateNormal)];
    self.zhSStr= @"";
   self.zhSEnd = @"";
    [self loadNewData];
}
- (void)tradeRecordsListRequest {
    
    WEAKSELF
    if (_pageNum == 1) {
        [self.dataArray removeAllObjects];
    }
    
    NSDictionary *params;
    NSString *urlString = @"";

    params = @{
        @"pageNum": [NSNumber numberWithInteger:_pageNum],
        @"pageSize": DEFULT_LOAD_NUM,
        @"Authorization":[UserDefaultUtil valueForKey:USER_TOKEN],
        @"gmtModifiedStart" : self.zhSStr,
        @"gmtModifiedEnd" : self.zhSEnd,
        @"accountType":self.accountType
    };
    
    urlString = [NSString stringWithFormat:@"%@/ntocc-basic-api/shipmentPaidRecord/drives/%@", [ReAddressDynamic shared].baseUrl, [UserDefaultUtil valueForKey:Login_Branch][@"code"]];
            
    [HTTPManager getNetworkRequest:urlString withParameter:params withVC:self netName:@"收款记录列表" success:^(id response, NSString *msg) {
        
        if ([response[@"records"] count] != 0) {
            [weakSelf.dataArray addObjectsFromArray:[CollationRecordModel mj_objectArrayWithKeyValuesArray:response[@"records"]]];
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshing];
            
            weakSelf.foundImageView.hidden = YES;
            weakSelf.tableView.hidden = NO;
            
            //                [KNotificationCenter postNotificationName:LOGIN_SUCCESS object:nil];
            
        } else {
            if (_pageNum == 1) {
                weakSelf.foundImageView.hidden = NO;
                weakSelf.tableView.hidden = YES;
            }
            
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            
        }
        
        [weakSelf.tableView reloadData];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

//上拉刷新数据
- (void)loadNewData{
    _pageNum = 1;
    [self tradeRecordsListRequest];
}

- (void)loadMoreData{
    _pageNum ++;
    [self tradeRecordsListRequest];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TradeRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.colltionModel = self.dataArray[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}

#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, CGRectGetMaxY(self.zhStatTimeButton.frame) + 5, ScreenW, ScreenH - NavHeight - MenuBarHeight - 55);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TradeRecordCell class]) bundle:nil] forCellReuseIdentifier:cellID];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
- (UIImageView *)foundImageView {
    if (!_foundImageView) {
        _foundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 170, 170)];
        _foundImageView.image = image(@"gcy_img_list_found");
        _foundImageView.contentMode = UIViewContentModeScaleAspectFit;
        _foundImageView.center = self.view.center;
        _foundImageView.centerY -= 60;
        [self.view addSubview:_foundImageView];
    }
    return _foundImageView;
}
-(UIButton *)zhStatTimeButton{
    if (!_zhStatTimeButton) {
        self.zhStatTimeButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
        self.zhStatTimeButton.frame = CGRectMake( 25 , 5, (ScreenW - 100)/2, 40);
        self.zhStatTimeButton.layer.masksToBounds = YES;
        self.zhStatTimeButton.layer.cornerRadius = 10;
        self.zhStatTimeButton.layer.borderWidth = 1;
        self.zhStatTimeButton.layer.borderColor = [Light_TextColor CGColor];
        self.zhStatTimeButton.tag = 100;
        [self.zhStatTimeButton addTarget:self action:@selector(zhButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.zhStatTimeButton setTitleColor:Light_TextColor forState:UIControlStateNormal];
        [self.zhStatTimeButton setTitle:@"收款开始时间" forState:UIControlStateNormal];
    }
    return _zhStatTimeButton;
}
- (UILabel *)zhZhiLabel{
    if (!_zhZhiLabel) {
        self.zhZhiLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.zhStatTimeButton.frame) + 10, 5, 30, 40)];
        self.zhZhiLabel.text = @"至";
        self.zhZhiLabel.textAlignment = NSTextAlignmentCenter;
        self.zhZhiLabel.textColor = Light_TextColor;
        self.zhZhiLabel.font = [UIFont systemFontOfSize:15];
    }
    return _zhZhiLabel;
}
-(UIButton *)zhEndTimeButton{
    if (!_zhEndTimeButton) {
        self.zhEndTimeButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
        self.zhEndTimeButton.frame = CGRectMake(CGRectGetMaxX(self.zhZhiLabel.frame) + 10, 5, (ScreenW - 100)/2, 40);
        self.zhEndTimeButton.layer.masksToBounds = YES;
        self.zhEndTimeButton.layer.cornerRadius = 10;
        self.zhEndTimeButton.layer.borderWidth = 1;
        self.zhEndTimeButton.layer.borderColor = [Light_TextColor CGColor];
        self.zhEndTimeButton.tag = 101;
        [self.zhEndTimeButton addTarget:self action:@selector(zhButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.zhEndTimeButton setTitleColor:Light_TextColor forState:UIControlStateNormal];
        [self.zhEndTimeButton setTitle:@"收款结束时间" forState:UIControlStateNormal];
    }
    return _zhEndTimeButton;
}
- (void)zhButtonAction:(UIButton *)btn{
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]initWithPickerMode:BRDatePickerModeYMDE];
    datePickerView.title = @"选择日期";
    datePickerView.isAutoSelect = NO;
    datePickerView.showToday = NO; // 是否显示“今天”
    datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
        if (btn.tag == 100) {
            [self.zhStatTimeButton setTitle:selectValue forState:(UIControlStateNormal)];
            self.zhSStr = selectValue;
            [self loadNewData];
        }else if (btn.tag == 101){
            [self.zhEndTimeButton setTitle:selectValue forState:(UIControlStateNormal)];
            self.zhSEnd = selectValue;
            [self loadNewData];
        }
    };
    [datePickerView show:nil];
   
}
@end
